package Windows.Share;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeSelectionModel;

import Client.msgSend;
import GroupWare.ShareFile;
import Tools.InfoFormat;
import Windows.MainWindows;
import Windows.Tree.PanelTreeNodeInterface;
import Windows.Tree.TreeNodePic;
import XML.XMLNode;

public class ResShareTable extends JScrollPane implements MouseListener, TreeSelectionListener, PanelTreeNodeInterface {
	JTable table = new JTable();
	ResShareTableModel mt = new ResShareTableModel(table);
	msgSend send;
	TimerTask tt;
	JPopupMenu menu = new JPopupMenu();
	Timer timer = new Timer();
	TreeNodePic shareNode;

	private boolean mouseClick = false;

	public ResShareTable() {
		shareNode = MainWindows.getInstance().client.sp;
		System.out.println("[ResShareTable]" + shareNode);
		this.send = MainWindows.getInstance().client;
		setViewportView(table);
		init();
		table.getColumn("图标").setMaxWidth(30);// 修改列宽
		// TableColumn tc = table.getColumn("速度");
		table.setPreferredScrollableViewportSize(new Dimension(550, 30));

//		tt = new TimerTask() {
//
//			public void run() {
//				// TODO Auto-generated method stub
//				table.repaint();// 重新绘制
//			}
//
//		};
//		timer.schedule(tt, 500, 500);

		table.getColumn("图标").setMaxWidth(30);// 修改列宽
		table.getColumn("类型").setMaxWidth(60);// 修改列宽
		table.getColumn("评论").setMaxWidth(50);// 修改列宽
		table.getColumn("文件大小").setMaxWidth(60);// 修改列宽
		table.getColumn("更新日期").setMaxWidth(180);// 修改列宽
		table.getColumn("资源").setMaxWidth(60);// 修改列宽
		table.getColumn("MD5").setMaxWidth(180);// 修改列宽
	}

	/**
	 * 删除任务，包括集合中
	 */
	public void deleteFile(int row) {
		mt.getSfDir().Delete(row);// 删除文件
		table.updateUI();
	}

	public void doubleMouse() {
		ShareFile sf = getSelectFile();
		if (sf != null) {
			if (sf.getF().isFile())
				sf.open();// 打开文件
			else
				mt.setSfDir(sf);
		}
	}

	public ShareFile getSelectFile() {
		int i = getSelectRow();
		if (i == -1)
			return null;
		return mt.getFile(i);
	}

	public int getSelectRow() {
		return table.getSelectedRow();
	}

	@Override
	public TreeNodePic getTreeNode() {
		return shareNode;
	}

	public void init() {

		// RatioRenderer renderer = new RatioRenderer();
		// renderer.setForeground(Color.BLUE);
		// table.setDefaultRenderer(Dimension.class, renderer);

		JMenuItem opf = new JMenuItem("打开文件");
		JMenuItem opd = new JMenuItem("打开目录");
		JMenuItem dele = new JMenuItem("删除文件");
		JMenuItem attribute = new JMenuItem("资源信息");
		JMenuItem comment = new JMenuItem("评论资源");
		menu.add(opf);
		menu.add(opd);
		menu.add(dele);
		menu.add(attribute);
		menu.add(comment);

		// --注册事件监听器
		table.addMouseListener(this);
		Tools.TableStyle.setDefaultStyle(table);
		table.getColumn("图标").setPreferredWidth(5);

		opf.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				// xmlFlag xf = getSelectXml();
				doubleMouse();
			}
		});

		opd.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				// xmlFlag xf = getSelectXml();
				int i = getSelectRow();
				if (i != -1) {
					mt.getFile(getSelectRow()).openDir();// 打开文件夹
				}
			}
		});

		dele.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				// xmlFlag xf = getSelectXml();
				int i = getSelectRow();
				if (i != -1) {
					int k = JOptionPane.showConfirmDialog(null, "您确认删除这个文件吗？");
					if (k == 0) {
						deleteFile(i);
					}
				}
			}
		});
		attribute.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				ShareFile s = getSelectFile();
				if (s != null && s.getF().isFile()) {
					String md5 = s.md5;
					String type = InfoFormat.getTypeStr(s.getF().getName());
					String Title = s.getF().getName();
					String lon = String.valueOf(s.getF().length());
					Integer inte = new Integer(0);
					XMLNode xn = new XMLNode(null, "FILE");
					xn.put("md5", md5);
					xn.put("name", Title);
					xn.put("size", lon);
					xn.put("Pip", "1");
//					System.out.println(xn);
					MainWindows.getResInfo().display(xn);
				}
			}
		});
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent m) {
		// TODO Auto-generated method stub
		// System.out.println("下载表格:鼠标按钮释放，"+m.getButton());
		if (m.getButton() == 3 && this.getSelectRow() != -1) {
			menu.show(this, m.getX(), m.getY() + 10);
		}

		if (m.getButton() == 1 && this.getSelectRow() != -1) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					if (mouseClick) {
						doubleMouse();
						mouseClick = false;
						return;
					}
					mouseClick = true;
					try {
						Thread.sleep(300);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					mouseClick = false;

				}
			}).start();

		}

	}

	public void tableChanged(TableModelEvent e) {

	}

	public void up() {
		if (mt.getSfDir() != null && !mt.getSfDir().getF().getAbsoluteFile().equals(MainWindows.getInstance().client.sp.getF().getAbsoluteFile())) {
			mt.setSfDir(new ShareFile(mt.getSfDir().getF().getParentFile()));
			table.updateUI();
		}

	}

	@Override
	public void valueChanged(TreeSelectionEvent tse) {
		// TODO Auto-generated method stub
		JTree jt = (JTree) tse.getSource();
		TreeSelectionModel tsm = jt.getSelectionModel();
		tsm.getMinSelectionRow();
		String str = tse.getPath().toString();
		// [ResShare, 共享资源, test]
		String head = "[ResShare, 共享目录";
		if (str.startsWith(head)) {
			str = str.substring(head.length(), str.length() - 1).replaceAll(", ", "/");
			ShareFile sf = new ShareFile(new File(MainWindows.getInstance().client.sp.getF().getAbsolutePath() + str));
			mt.setSfDir(sf);
			table.updateUI();
		}
	}
}
